// 调试工具页面
const app = getApp();
const envConfig = require('../../config/env.js');
const debugTools = require('../../utils/debugTools.js');

Page({
  data: {
    systemInfo: {},
    apiEndpoints: {},
    networkStatus: {},
    performanceData: {},
    storageInfo: {}
  },
  
  onLoad() {
    this.gatherDebugInfo();
  },
  
  gatherDebugInfo() {
    // 创建计时器
    const timer = new debugTools.PerformanceTimer('收集调试信息');
    
    // 获取系统信息
    this.getSystemInfo();
    timer.mark('系统信息');
    
    // 获取API端点信息
    this.getApiEndpoints();
    timer.mark('API端点');
    
    // 获取网络状态
    this.getNetworkStatus();
    timer.mark('网络状态');
    
    // 获取存储信息
    this.getStorageInfo();
    timer.mark('存储信息');
    
    // 记录计时数据
    this.setData({
      performanceData: timer.end()
    });
  },
  
  // 获取系统信息
  getSystemInfo() {
    try {
      const systemInfo = wx.getSystemInfoSync();
      this.setData({ systemInfo });
    } catch (e) {
      console.error('获取系统信息失败:', e);
    }
  },
  
  // 获取API端点
  getApiEndpoints() {
    const api = require('../../utils/api.js');
    const apiEndpoints = {
      baseUrl: api.getBaseURL ? api.getBaseURL() : '未提供',
      plantIdentify: api.plantIdentify,
      localDiseaseInfo: api.localDiseaseInfo
    };
    this.setData({ apiEndpoints });
  },
  
  // 获取网络状态
  getNetworkStatus() {
    wx.getNetworkType({
      success: (res) => {
        this.setData({ networkStatus: res });
      }
    });
  },
  
  // 获取存储信息
  getStorageInfo() {
    wx.getStorageInfo({
      success: (res) => {
        this.setData({ storageInfo: res });
      }
    });
  },
  
  // 清除本地存储
  clearStorage() {
    wx.showModal({
      title: '确认操作',
      content: '确定要清除所有本地存储数据吗？这将导致用户退出登录，并清除所有缓存。',
      success: (res) => {
        if (res.confirm) {
          wx.clearStorage({
            success: () => {
              wx.showToast({
                title: '存储已清除',
                icon: 'success'
              });
              setTimeout(() => {
                this.getStorageInfo();
              }, 500);
            }
          });
        }
      }
    });
  },
  
  // 返回上一页
  goBack() {
    wx.navigateBack();
  }
});
